#!/usr/bin env python
# -*- coding: utf-8 -*-
###########################################################################
#   Copyright (C) 2008 by Serj Poltavsky                                  #
#   serge.uliss at gmail.com                                              #
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 3 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
#   This program is distributed in the hope that it will be useful,       #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#   GNU General Public License for more details.                          #
#                                                                         #
#   You should have received a copy of the GNU General Public License     #
#   along with this program. If not, see <http://www.gnu.org/licenses/>   #
###########################################################################

import os
from dvdobject import DvdContainer
from dvdsound import DvdSound
from dvdimage import DvdImage

class DvdMenuPage(DvdContainer):
    _count = 0

    def __init__(self, format):
        DvdContainer.__init__(self, format)
        self._count += 1
        self.count = self._count

        self.bgsound = DvdSound(None, format)
        self.bgimage = DvdImage(None, format)

        self.bgvideo = None
        self.buttons_image = None
        self.buttons_image_highlight = None
        self.buttons_image_select = None


    def export(self, menu):
        self.export_buttons()
        self.export_bgimage()
        self.export_bgsound()
        self.export_bgvideo()
        self.export_mplex()
        self.export_spumux()
        self.export_pgc(menu)

    def export_bgimage(self):
        self.bgimage.export()

        from tools.image import ImageCombine

        icomb = ImageCombine(self.format.size)
        icomb.add(self.bgimage.filename)
        if self.buttons_image:
            icomb.add(self.buttons_image)
 
        icomb.save(self.tmp_bgimage())
        self.bgimage.filename = self.tmp_bgimage()

    def export_bgsound(self):
        self.bgsound.export()

        # no convert needed
        if self.bgsound.aformat in ('mp2', 'ac3'):
            return

        from tools.sound import SoundConvert
        snd_conv = SoundConvert(self.bgsound.filename, self.tmp_bgsound('mp2'))
        snd_conv.run()
        self.bgsound.filename = snd_conv.dest

    def export_bgvideo(self):
        from tools.image import ImageConvert
        from dvdvideo import DvdVideo
        imconv = ImageConvert(self.bgimage.filename, self.tmp_bgvideo(), self.format.format)
        imconv.run()
        self.bgvideo = DvdVideo(imconv.dest, self.format)

    def export_buttons(self):
        # no buttons
        if not len(self):
            return

        from tools.image import ImageCombine
        icomb = ImageCombine(self.format.size)
        hcomb = ImageCombine(self.format.size, None, True)
        scomb = ImageCombine(self.format.size, None, True)
        for button in self:
            button.export()
            icomb.add(button.tmp_normal(), button.pos)
            hcomb.add(button.tmp_highlight(), button.pos)
            scomb.add(button.tmp_select(), button.pos)
        icomb.save(self.tmp_buttons_image())
        hcomb.save(self.tmp_buttons_highlight())
        scomb.save(self.tmp_buttons_select())
        self.buttons_image_highlight = self.tmp_buttons_highlight()
        self.buttons_image_select = self.tmp_buttons_select()

    def export_mplex(self):
        from tools.multiplex import Multiplex
        mplex = Multiplex(self.bgvideo.filename, self.bgsound.filename, self.tmp_mplex())
        mplex.run()
        self.bgvideo.filename = mplex.dest

    def export_pgc(self, menu):
        from dvdauthor import PGC, VOB, Button
        pgc = PGC(VOB(self.bgvideo.filename))

        for button in self:
            b = Button(button.title)
            #b.append('jump title 1;')
            pgc.append(b)

        menu.append(pgc)

    def export_spumux(self):
        from spumux import Spumux, SpumuxStream, SpumuxButton
        from tools.spumuxconvert import SpumuxConvert

        spumux = Spumux()
        stream = SpumuxStream(None, self.buttons_image_highlight, self.buttons_image_select)
        stream.force = "yes"

        for button in self:
            b = SpumuxButton(button.title, button.pos, button.size)
            stream.append(b)

        spumux.append(stream)
        spumux.update()
        spumux.save(self.tmp_spumux_xml())

        sconv = SpumuxConvert(self.bgvideo.filename, self.tmp_spumux_out(), self.tmp_spumux_xml())
        sconv.run()
        self.bgvideo.filename = sconv.dest

    def get_bgsound(self):
        return self.__bgsound

    def set_bgsound(self, sound):
        if isinstance(sound, DvdSound):
            self.__bgsound = sound
        else:
            raise TypeError("Non DvdSound instance given")

    def tmp_bgimage(self):
        return os.path.join(self.tmp_dir(), "menu_page_%03i_bgimage.png" % self.count)

    def tmp_bgsound(self, ext):
        return os.path.join(self.tmp_dir(), "menu_page_%03i_bgsound.%s" % (self.count, ext))

    def tmp_bgvideo(self):
        return os.path.join(self.tmp_dir(), "menu_page_%03i_bgvideo.mpg" % self.count)

    def tmp_buttons_image(self):
        return os.path.join(self.tmp_dir(), "menu_page_%03i_buttons.png" % self.count)

    def tmp_buttons_highlight(self):
        return os.path.join(self.tmp_dir(), "menu_page_%03i_buttons_highlight.png" % self.count)

    def tmp_buttons_select(self):
        return os.path.join(self.tmp_dir(), "menu_page_%03i_button_select.png" % self.count)

    def tmp_mplex(self):
        return os.path.join(self.tmp_dir(), "menu_page_%03i_mplex.mpg" % self.count)

    def tmp_spumux_xml(self):
        return os.path.join(self.tmp_dir(), "menu_page_%03i_spumux.xml" % self.count)

    def tmp_spumux_out(self):
        return os.path.join(self.tmp_dir(), "menu_page_%03i_spumux.mpg" % self.count)

    bgsound = property(get_bgsound, set_bgsound)